package com.dhj.yygh.controller;

import com.dhj.yygh.common.exception.YyghException;
import com.dhj.yygh.common.result.MyResult;
import com.dhj.yygh.service.RlySmsUService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@Api(tags = "短信服务相关接口")
@RestController
@RequestMapping("/api/sms")
@Slf4j
public class SmsController {
    @Autowired
    private RlySmsUService rlySmsUService;

    @GetMapping("/sendSms/{phone}")
    public MyResult<Map<String, Object>> sendSms(
            @PathVariable("phone") String phone) {

        Map<String, Object> map;
        try {
            map = rlySmsUService.sendSms(phone);
            if ((boolean) map.get("sendState")) {
                return MyResult.ok(map);
            }
            return MyResult.fail(map);
        } catch (YyghException e) {
            e.printStackTrace();
            map = new HashMap<>();
            map.put("sendState", false);
            map.put("msg", e.getMessage());
            return MyResult.fail(map);
        }
    }
}
